package com.gxwz.medical.domain;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.gxwz.medical.database.DbUtil;
/**
 * 慢病证信息管理模块类
 * @author 演示
 *
 */
public class ChronicInfo {
	/**
	 * 农合证号
	 */
    private String perscode;
	/**
	 * 疾病编码
	 */
	private String illcode;	
	/**
	 * 身份证号
	 */
	private String cardID;
	/**
	 * 慢性病
	 */
	private String illname;
	/**
	 * 慢病起始时间
	 */
	private String starttime;
	/**
	 * 慢病治愈时间
	 */
	private String endtime;
	
	public ChronicInfo (){
		
	}
	public ChronicInfo(String perscode)throws SQLException{
		try{
		this.perscode=perscode;
		loadDB(perscode);
	    }catch(SQLException e){
	    	throw e;
	    }
		
	}
	
	private void loadDB(String perscode)throws SQLException{
		Connection conn =null;
		PreparedStatement pstmt=null;
		ResultSet rs=null;
		try{
			conn=DbUtil.getConn();
			pstmt=conn.prepareStatement("select * from t_chronicInfo where perscode=?");
			int index=1;
			pstmt.setString(index, this.perscode);
			rs=pstmt.executeQuery();
			if(rs.next()){
				this.perscode=rs.getString("perscode");
				this.illcode =rs.getString("illcode");
				this.cardID =rs.getString("cardID");
				this.illname =rs.getString("illname");
				this.starttime =rs.getString("starttime");
				this.endtime =rs.getString("endtime");
				
			}
		}catch(SQLException e){
			throw e;
		}finally{
			DbUtil.close(rs, pstmt, conn);
		}
	}
	
	public ChronicInfo(String perscode, String illcode, String cardID, String illname,String starttime, String endtime) {
		super();
		this.perscode = perscode;
		this.illcode = illcode;
		this.cardID = cardID;
		this.illname = illname;
		this.starttime = starttime;
		this.endtime = endtime;
	}

	
	/**
	 * 添加慢病证信息
	 * @throws Exception
	 */
	
	public void add()throws Exception{
		Connection conn=null;
		try{
			//1:对象属性赋值,无
    	    conn=DbUtil.getConn();
    	   //2；调用saveToDB方法保存到数据库
    	    saveToDB(conn);
		}catch(SQLException e){
	    	   throw e;
	       }finally{
	    	   DbUtil.close(conn);
	       }
		
	}
	/**
	 * 持久化数据
	 * @param conn
	 * @throws SQLException
	 */
	private void saveToDB(Connection conn)throws SQLException{

		  PreparedStatement pstmt=null;
		  try{
			  StringBuffer sqlBuff=new StringBuffer("INSERT INTO t_chronicInfo (perscode,illcode,cardID,illname,starttime,endtime)");
			  			   sqlBuff.append("VALUES (?, ?, ?, ?, ?, ?)");
			  pstmt=conn.prepareStatement(sqlBuff.toString());
			  int index=1;
			  pstmt.setString(index++, this.perscode);
			  pstmt.setString(index++, this.illcode);
			  pstmt.setString(index++, this.cardID);
			  pstmt.setString(index++, this.illname);
			  pstmt.setString(index++, this.starttime);
			  pstmt.setString(index++, this.endtime);
			  pstmt.addBatch();
			  int count =pstmt.executeUpdate(); 
		  }catch(SQLException e){
			  e.printStackTrace();
			  throw e;
		  }finally{
			  DbUtil.close(pstmt);
		  } 
}
	

	/**
	 * 修改慢病证信息
	 * @throws Exception
	 */
	
	public void edit()throws SQLException{
		Connection conn =DbUtil.getConn();
		try{
			 conn.setAutoCommit(false);
			 this.updateToDB(conn);
			 conn.commit();
		}catch(SQLException e){
			conn.rollback();
			throw e;
		}finally{
			DbUtil.close(conn);
		}
	}
	private void updateToDB(Connection conn)throws SQLException{
		PreparedStatement pstmt=null;
		try{
			//定义构造update SQL语句字符串变量sql
			String  sql="update t_chronicInfo set illcode=?,cardID=?,illname=?,starttime=?,endtime=? where perscode=?";
			 //创建执行带动态参数的SQL的PreparedStatement pstmt
			 pstmt=conn.prepareStatement(sql);
			 //设置动态参数对应的值
			 int index=1;
			 pstmt.setString(index++, this.illcode);
			 pstmt.setString(index++, this.cardID);
			 pstmt.setString(index++, this.illname);
			 pstmt.setString(index++, this.starttime);
			 pstmt.setString(index++, this.endtime);
			 pstmt.setString(index++, this.perscode);
			 int count=pstmt.executeUpdate();
			
		}catch(SQLException e){
			throw e;
		}finally{
			DbUtil.close(pstmt);
		}
		
	}
	
	
	public void del(String perscode)throws SQLException{

		Connection conn=null;
       try{
    	   //1:对象属性赋值
    	    this.perscode =perscode;
    	    conn=DbUtil.getConn();
    	   //2；调用delFromDB方法
    	    delFromDB(conn);
       }catch(SQLException e){
    	   throw e;
       }finally{
    	   DbUtil.close(conn);
       }
		
	}
	private void delFromDB(Connection conn)throws SQLException{
		PreparedStatement pstmt=null;
		try{
			//定义构造插入SQL语句字符串变量sql
			String  sql="delete from t_chronicInfo where perscode=?";
			 //创建执行带动态参数的SQL的PreparedStatement pstmt
			 pstmt=conn.prepareStatement(sql);
			 //设置动态参数对应的值
			 int index=1;
			 pstmt.setString(index++, this.perscode);
		
			int count=pstmt.executeUpdate();
			
		}catch(SQLException e){
			throw e;
		}finally{
			DbUtil.close(pstmt);
			
		}
		
		
	}


	public String getStarttime() {
		return starttime;
	}
	public void setStarttime(String starttime) {
		this.starttime = starttime;
	}
	public String getEndtime() {
		return endtime;
	}
	public void setEndtime(String endtime) {
		this.endtime = endtime;
	}

	public String getPerscode() {
		return perscode;
	}
	public void setPerscode(String perscode) {
		this.perscode = perscode;
	}
	public String getIllcode() {
		return illcode;
	}
	public void setIllcode(String illcode) {
		this.illcode = illcode;
	}
	public String getCardID() {
		return cardID;
	}
	public void setCardID(String cardID) {
		this.cardID = cardID;
	}
	public String getIllname() {
		return illname;
	}
	public void setIllname(String illname) {
		this.illname = illname;
	}
	

}
